#!/usr/bin/env bash
set -eu -o pipefail

if [ -v READYSET_SUBPROJECT_ROOT ]; then
  cd "$READYSET_SUBPROJECT_ROOT"
fi

if [ "$BUILDKITE_AGENT_META_DATA_QUEUE" = "apple" ]; then
  exit 0
fi

# Ensure that the `target` and `cargo-registry` volumes exist, so that steps
# that expect them to already exist (the ones that use docker-compose) can run
# even if they're the first steps to run on an agent
docker volume create target || true
docker volume create cargo-registry || true

if [ -n "${BUILDKITE_GROUP_LABEL+x}" ] && [ "$BUILDKITE_GROUP_LABEL" == "Framework Tests" ]; then
    ## Setup for framework testing
    mkdir -pv .buildkite/gen .buildkite/image
    buildkite-agent artifact download '.buildkite/gen/*' ./ || true
    buildkite-agent artifact download '.buildkite/image/**/*.tar.gz' ./ || true
    find .buildkite/image -type f | while read -r tarball; do
        docker load < "$tarball"
    done
fi
